package com.android.SchoolManagerSystem.action;

import java.net.HttpURLConnection;
import java.util.HashMap;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

import com.android.SchoolManagerSystem.CommonInfo;
import com.android.SchoolManagerSystem.data.Curricula;
import com.android.SchoolManagerSystem.data.InternShip;
import com.android.SchoolManagerSystem.data.Userinfo;
import com.android.SchoolManagerSystem.net.HttpRequester;
import com.android.SchoolManagerSystem.net.HttpResponser;


/**
 * get curricular info action
 * @param Userinfo
 * 		required, get info by user info 
 * 
 * @author oracle
 * @date 2011-7
 */

public class GetCurriculaInfo {
	private Userinfo 	curinfo = null;
	private Curricula 	Cculatmp = null;
	private boolean 	berrorcode = true;
	
	public final static int OPER_QUERY = 0;
	public final static int OPER_MODIFY = OPER_QUERY + 1;
	
	private String strSemester = null;
	private String stryear = null;


	public GetCurriculaInfo(Userinfo mCurUserinfo, String strSemesterin,
			String stryearin) {
		curinfo = mCurUserinfo;
		strSemester = strSemesterin;
		stryear =stryearin;
	}


	public boolean go(int arg) {
		String strHosturi = CommonInfo.strGetTranscripturi;
		String strrequetstXMl = generatexmlstring(arg);
		
		switch(arg)
		{
			case OPER_QUERY:
				strHosturi = CommonInfo.strGetCurriculauri;
				break;
			case OPER_MODIFY:
				strHosturi = CommonInfo.strSetChoosedCculauri;
				break;
			default:
				strHosturi = CommonInfo.strGetCurriculauri;
					break;
		}
		
		HashMap<String, String> propertys = new HashMap<String, String>();
		propertys.put("Set-Cookie", "ONE_VER=1_0;sid=10006");
		propertys.put("H-Product", "client");
		HttpRequester loginrequest = new HttpRequester();
		HttpResponser loginresponse = loginrequest.sendPost(
				strHosturi, propertys, strrequetstXMl);
		if (loginresponse == null
				|| loginresponse.getCode() != HttpURLConnection.HTTP_OK)
			return false;
		else {
			if (Decodesvrreponse(arg,loginresponse))
				return true;
			else
				return false;
		}
	}
	/**
	 * Send http request, and return http response
	 * 
	 * @author jeson_li
	 * @date 2011-3-28
	 */
	private boolean Decodesvrreponse(int arg,HttpResponser response )
	{
		boolean bres = false;
		switch(arg)
		{
			case OPER_QUERY:
				bres = decodeCCulaList(response);
				break;
			case OPER_MODIFY:
			default:
				bres = decodeUpdateRes(response);
				break;
		}
		
		return bres;
	}
	
	
	private String generatexmlstring(int arg) {
		String strlogreq = null;
		switch(arg)
		{
			case OPER_QUERY:
			default:
				 strlogreq = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
					+ "<queryallcculas apiversion=\"1.0\"><userid>"
					+ curinfo.struserid + "</userid><year>"
					+ stryear + "</year><Semester>"
					+ strSemester + "</Semester></queryallcculas>";
				break;
				
			case OPER_MODIFY:
				strlogreq = "<?xml version=\"1.0\" encoding=\"utf-8\"?><modifyculas apiversion=\"1.0\"><userid>"
							+curinfo.struserid
							+"</userid><year>"
							+ stryear + "</year><Semester>"
							+ strSemester +"</Semester>";
				for(int i = 0;i <curinfo.MyCurricula.size();i++)
				{
					Curricula cculatmp = curinfo.MyCurricula.get(i);
					if(cculatmp.bChoosed)
					{
						String strCulaNode = "<culaid>"+cculatmp.strCurriculaid+"</culaid>";
						strlogreq += strCulaNode;
					}
				}
				strlogreq += "</modifyculas>";
				break;
		}		

		return strlogreq;
	}
	
	private boolean decodeUpdateRes(HttpResponser repsonse) {
		 //String testxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><queryallccular apiversion=\"1.0\" maxselectnum=\"2\"> <cularitem><culaid>11234</culaid><status>0</status><culaname>math</culaname></cularitem><cularitem><culaid>4342</culaid><status>0</status><culaname>photograph</culaname></cularitem><cularitem><culaid>4234242</culaid><status>1</status><culaname>English</culaname></cularitem></queryallccular>";
		RootElement root = new RootElement("requestinternshipr");

		root.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
			}
		});
		root.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
			}
		});

		root.getChild("errorcode").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						int ires = body.compareTo("0");
						if(ires == 0)
							berrorcode = false;
						else
							berrorcode = true;
					}
				});

		try {
			Xml.parse(repsonse.getContent(), root.getContentHandler());
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		if (berrorcode)
			return false;
		else
			return true;
	}
	
	

	private boolean decodeCCulaList(HttpResponser repsonse) {

		//String testxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><queryallccular apiversion=\"1.0\" maxselectnum=\"2\"> <cularitem><culaid>11234</culaid><status>0</status><culaname>math</culaname></cularitem><cularitem><culaid>4342</culaid><status>0</status><culaname>photograph</culaname></cularitem><cularitem><culaid>4234242</culaid><status>1</status><culaname>English</culaname></cularitem></queryallccular>";
		RootElement root = new RootElement("queryallscorer");

		root.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
			}
		});
		root.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
			}
		});

		Element e = root.getChild("cularitem");

		e.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
				Cculatmp = new Curricula();
				berrorcode = false;
			}
		});

		e.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
				curinfo.MyCurricula.add(Cculatmp);
			};
		});
		

		e.getChild("culaid").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
					
						// TODO Auto-generated method stub
						if (body != null) {
							
							Cculatmp.strCurriculaid = body;
						}
					}
				});
		e.getChild("culaname").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							Cculatmp.strCurriculaName = body;
						}
					}
				});
		
		e.getChild("status").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							int ires = body.compareTo("1");
							if(ires == 0)
								Cculatmp.bChoosed = true;
							else
								Cculatmp.bChoosed = false;

						}
					}
				});

		e.getChild("errorcode").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						int ires = body.compareTo("0");
						if(ires != 0)
							berrorcode = true;
					}
				});

		try {
			Xml.parse(repsonse.getContent(),root.getContentHandler());
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		if (berrorcode)
			return false;
		else
			return true;
	}
}
